package org.rsbot.script.wrappers;

import java.util.Arrays;
import java.util.EnumSet;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.Players;
import org.rsbot.script.methods.Walking;

public class RSTilePath extends RSPath
{
  protected RSTile[] tiles;
  protected RSTile[] orig;
  private boolean end;

  public RSTilePath(MethodContext paramMethodContext, RSTile[] paramArrayOfRSTile)
  {
    super(paramMethodContext);
    this.orig = paramArrayOfRSTile;
    this.tiles = ((RSTile[])Arrays.copyOf(paramArrayOfRSTile, paramArrayOfRSTile.length));
  }

  public boolean traverse(EnumSet<RSPath.TraversalOption> paramEnumSet)
  {
    RSTile localRSTile1 = getNext();
    if (localRSTile1 == null) {
      return false;
    }
    if (localRSTile1.equals(getEnd())) {
      if ((this.methods.calc.distanceTo(localRSTile1) <= 1) || ((this.end) && (this.methods.players.getMyPlayer().isMoving())) || (localRSTile1.equals(this.methods.walking.getDestination()))) {
        return false;
      }
      this.end = true;
    } else {
      this.end = false;
    }
    if ((paramEnumSet.contains(RSPath.TraversalOption.HANDLE_RUN)) && (!this.methods.walking.isRunEnabled()) && (this.methods.walking.getEnergy() > 50)) {
      this.methods.walking.setRun(true);
      sleep(300);
    }
    if (paramEnumSet.contains(RSPath.TraversalOption.SPACE_ACTIONS)) {
      RSTile localRSTile2 = this.methods.walking.getDestination();
      if ((localRSTile2 != null) && (this.methods.players.getMyPlayer().isMoving()) && (this.methods.calc.distanceTo(localRSTile2) > 5) && (this.methods.calc.distanceBetween(localRSTile1, localRSTile2) < 7.0D))
      {
        return true;
      }
    }
    return this.methods.walking.walkTileMM(localRSTile1, 0, 0);
  }

  public boolean isValid()
  {
    return (this.tiles.length > 0) && (getNext() != null) && (!this.methods.players.getMyPlayer().getLocation().equals(getEnd()));
  }

  public RSTile getNext()
  {
    for (int i = this.tiles.length - 1; i >= 0; i--) {
      if (this.methods.calc.tileOnMap(this.tiles[i])) {
        return this.tiles[i];
      }
    }
    return null;
  }

  public RSTile getStart()
  {
    return this.tiles[0];
  }

  public RSTile getEnd()
  {
    return this.tiles[(this.tiles.length - 1)];
  }

  public RSTilePath randomize(int paramInt1, int paramInt2)
  {
    for (int i = 0; i < this.tiles.length; i++) {
      this.tiles[i] = this.orig[i].randomize(paramInt1, paramInt2);
    }
    return this;
  }

  public RSTilePath reverse()
  {
    RSTile[] arrayOfRSTile = new RSTile[this.tiles.length];
    for (int i = 0; i < this.orig.length; i++) {
      arrayOfRSTile[i] = this.orig[(this.tiles.length - 1 - i)];
    }
    this.orig = arrayOfRSTile;
    arrayOfRSTile = new RSTile[this.tiles.length];
    for (i = 0; i < this.tiles.length; i++) {
      arrayOfRSTile[i] = this.tiles[(this.tiles.length - 1 - i)];
    }
    this.tiles = arrayOfRSTile;
    return this;
  }

  public RSTile[] toArray()
  {
    RSTile[] arrayOfRSTile = new RSTile[this.tiles.length];
    System.arraycopy(this.tiles, 0, arrayOfRSTile, 0, this.tiles.length);
    return arrayOfRSTile;
  }
}